Code execution tool
https://docs.claude.com/en/docs/agents-and-tools/tool-use/code-execution-tool
基本的に、ClaudeのAPI呼び出しをやるときに使うもの?mrsekut.icon
Claude Codeからも使えるかもしれないが、あまり意味なさそうか
下記は、Skillsの説明での記述
上記はClaudeチャットのウェブインターフェイスからClaude Skills を呼び出す時の話で、Claude Codeに登録して使うときはこのサンドボックスには該当しません。Claude Codeでは単にローカルでBashツールとしてコードを実行しています。ref
gpt-5.icon
Code Execution Tool は、Claude に 安全なサンドボックス環境でコードを実行させる機能 です。
これにより Claude は:
Bash コマンド(例: ls, pip install, python script.py)
ファイル操作(例: ファイル作成・編集・閲覧)
Python コードの実行(計算・解析・グラフ生成など)
を行い、結果を出力やファイルとして返す ことができます。
🧱 対応モデルと有効化
利用には API 呼び出し時にヘッダーを追加します:
code:http
anthropic-beta: code-execution-2025-08-25
対応モデル:
table:_
モデル名 ツールバージョン
Claude 3.7〜4.5(Opus/Sonnet/Haiku) code_execution_20250825
🧠 code_execution_20250522 は旧バージョンで「Python のみ」対応。
新版は Bash やファイル操作も可能。
⚙️ 実行の流れ
1. ツールをリクエストに指定:
code:json
"tools": "type": "code_execution_20250825", "name": "code_execution" }
2. Claude が「コードを実行したほうが良い」と判断した場合、自動的に実行環境が起動。
3. Bash やファイル操作が安全な Linux コンテナ内で実行される。
4. 結果(stdout / stderr / return code)が返却される。
💻 主な使い方
1. Bash コマンド実行
例:「Python のバージョンを確認」
code:bash
Check the Python version and list installed packages
Claude が内部で:
code:bash
python --version
pip list
を実行して結果を返す。
2. ファイルの作成・編集
例:「config.yaml を作成し、port を 5432→3306 に変更」
code:bash
Create a config.yaml file with database settings, then update the port from 5432 to 3306
Claude が:
config.yaml を生成
テキストを置換して編集
結果を JSON で返す
3. ファイルをアップロードして解析
Files API と組み合わせると、自分の CSV や Excel をアップロードして分析可能。
例:
code:python
file_object = client.beta.files.upload(file=open("data.csv", "rb"))
→ file_id を Claude に渡すと、
Claude が Pandas で分析・グラフ化して結果を返す。
4. ファイル生成物の取得
Claude が作成したファイル(例: output.png, report.csv)は
Files API 経由でダウンロード可能。
🔒 実行環境の仕様
table:_
項目 内容
OS Linux
Python 3.11
メモリ 1 GiB
ディスク 5 GiB
CPU 1 コア
ネットワーク 外部アクセス禁止(完全オフライン)
有効期限 30日
再利用 Container ID 指定で可能
🧠 Container 再利用
同じコンテナを再利用することで、前回作成したファイルを引き継げます。
code:python
response1 = client.beta.messages.create(...)
container_id = response1.container.id
response2 = client.beta.messages.create(
container=container_id,
...
)
→ これで一連の作業を継続して行える(状態が保持される)。
🧾 応答フォーマット
Claude からのレスポンスには以下のタイプがあります:
Bash 結果
code:json
{
"type": "bash_code_execution_tool_result",
"content": {
"stdout": "...",
"stderr": "",
"return_code": 0
}
}
ファイル操作結果
code:json
{
"type": "text_editor_code_execution_tool_result",
"content": {
"file_type": "text",
"content": "...file contents..."
}
}
エラー例
code:json
{
"error_code": "file_not_found" | "execution_time_exceeded" | "too_many_requests"
}
🧩 料金・制限
課金単位:実行時間ベース(5分単位)
価格:$0.05 / session-hour
トークン料金とは別枠